/**
 * 
 */
package dms.service.imp;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dms.dto.BuildingDTO;
import dms.model.Building;
import dms.model.Campus;
import dms.model.util.PageIterator;
import dms.repository.BuildingRepository;
import dms.repository.CampusRepository;
import dms.service.BuildingService;
import dms.util.DateFormatUtil;

/**
 * @author 李健
 *
 */
public class BuildingServiceImpl implements BuildingService {

	private static final Log log = LogFactory.getLog(BuildingServiceImpl.class);
	
	private BuildingRepository buildingRepository = null;
	private CampusRepository campusRepository = null;
	/**
	 * 
	 */
	public BuildingServiceImpl() {
		super();
	}

	/**
	 * @param buildingRepository
	 */
	public BuildingServiceImpl(BuildingRepository buildingRepository, CampusRepository campusRepository) {
		super();
		this.buildingRepository = buildingRepository;
		this.campusRepository = campusRepository;
	}

	/**
	 * @return the campusRepository
	 */
	public CampusRepository getCampusRepository() {
		return campusRepository;
	}

	/**
	 * @param campusRepository the campusRepository to set
	 */
	public void setCampusRepository(CampusRepository campusRepository) {
		this.campusRepository = campusRepository;
	}

	/**
	 * @return the buildingRepository
	 */
	public BuildingRepository getBuildingRepository() {
		return buildingRepository;
	}

	/**
	 * @param buildingRepository the buildingRepository to set
	 */
	public void setBuildingRepository(BuildingRepository buildingRepository) {
		this.buildingRepository = buildingRepository;
	}

	@Override
	public Building createBuilding(BuildingDTO buildingInfo) throws ParseException {
		Building building = new Building();
		
		Campus campus = campusRepository.findById(buildingInfo.getCampus());
		building.setCampus(campus);
		
		building.setBuildDate(DateFormatUtil.parseDate(buildingInfo.getBuildDate()));
		building.setCapacity(buildingInfo.getCapacity());
		building.setDescription(buildingInfo.getDescription());
		building.setDormitoryCount(buildingInfo.getDormitoryCount());
		building.setFloors(buildingInfo.getFloors());
		building.setName(buildingInfo.getName());
		
		return this.buildingRepository.createBuilding(building);
	}

	@Override
	public void deleteBuildingById(Long id) {
		this.buildingRepository.deleteBuildingById(id);
		
	}
	
	public void deleteBuildings(List<Long> ids) {
		this.buildingRepository.deleteBuildings(ids);
	}

	@Override
	public PageIterator<Building> findAll(int start, int count) {
		return this.buildingRepository.findAll(start, count);
	}

	@Override
	public PageIterator<Building> findByCampus(int start, int count,
			Campus campus) {
		return this.buildingRepository.findByCampus(start, count, campus);
	}

	@Override
	public Building findById(Long id) {
		return this.buildingRepository.findById(id);
	}

	@Override
	public Building updateBuilding(BuildingDTO buildingInfo) throws ParseException {
		Building building = this.buildingRepository.findById(buildingInfo.getId());
		Campus campus = this.campusRepository.findById(buildingInfo.getCampus());
		
		try {
			building.setBuildDate(DateFormatUtil.parseDate(buildingInfo.getBuildDate()));
		} catch (ParseException e) {
			throw e;
		}
		building.setCapacity(buildingInfo.getCapacity());
		building.setDescription(buildingInfo.getDescription());
		building.setDormitoryCount(buildingInfo.getDormitoryCount());
		building.setFloors(buildingInfo.getFloors());
		building.setName(buildingInfo.getName());
		
		building.setCampus(campus);
		
		return this.buildingRepository.updateBuilding(building);
	}

}
